package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author jingchun
 * @create 2024-07-10 9:52
 */

@Controller
public class IndexController {

	@Autowired
	private CategoryService categoryService;
	@Autowired
	RedissonClient redisson;

	@GetMapping({"/", "index.html"})
	public String indexPage(Model model) {

		//1.查出所有的一级分类
		List<CategoryEntity> categoryEntities =  categoryService.getLevel1Categorys();
		model.addAttribute("categories", categoryEntities);

		return "index";
	}
	@GetMapping(value = "/index/catalog.json")
	@ResponseBody
	public Map<String, List<Catelog2Vo>> getCatlogJson() {
		Map<String, List<Catelog2Vo>> catlogJson =	categoryService.getCatlogJson();
		return catlogJson;
	}

	@GetMapping(value = "/hello")
	@ResponseBody
	public String Hello() {
		//1.获取一把锁
		RLock lock = redisson.getLock("my-lock");
		//2.加锁
		lock.lock();
		try {
			System.out.println("加锁成功，执行业务..."+Thread.currentThread().getId());
			Thread.sleep(20000);
		}catch (Exception e){

		}finally {
			System.out.println("释放锁..."+Thread.currentThread().getId());
			lock.unlock();
		}

		return "hello";
	}



}
